/*
 * @Author: your name
 * @Date: 2021-02-03 16:02:33
 * @LastEditTime: 2021-02-03 23:15:59
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \wwwd:\凯哥购物项目\utils\asyncWx.js
 */
export const showModal = ({
    content
}) => {

    return new Promise((resolve, reject) => {
        wx.showModal({
            title: '提示',
            content: content,

            success: (result) => {
                resolve(result);
            },
            fail: (err) => {
                reject(err);
            }

        });
    })
}

export const showToast = ({
    title
}) => {

    return new Promise((resolve, reject) => {
        wx.showToast({
            title: title,
            icon: '',

            success: (result) => {
                resolve(result);
            },
            fail: (err) => {
                reject(err);
            }

        });
    })
}


//微信登录

export const login = () => {

    return new Promise((resolve, reject) => {
        wx.login({
            success: function(res) {
                resolve(res);
            },
            fail: function(err) {
                reject(err);
            },
            complete: function() {

            }
        })
    })
}

/*
微信支付
*/
export const requestPayment = (pay) => {

    return new Promise((resolve, reject) => {
        wx.requestPayment({
            ...pay,

            success: (result) => {
                resolve(result);

            },
            fail: (err) => {
                reject(err);
            }

        });
    })
}

export const getStorage = (key) => {

    return new Promise((resolve, reject) => {
        wx.getStorage({
            key: key,
            success: function(res) {
                resolve(res);
            },
            fail: function(err) {
                reject(err);
            }

        });
    })
}

export const userOpenid = (key) => {

    return new Promise((resolve, reject) => {
        wx.getStorage({
            key: key,
            success: function(res) {
                resolve(res);
            },
            fail: function(err) {
                reject(err);
            }

        });
    })
}

export const getOpenid = (code) => {

    return new Promise((resolve, reject) => {
        var reqTask = wx.request({


            url: 'https://api.weixin.qq.com/sns/jscode2session',
            data: {
                appid: "wx3a939ab0dd34e12d",
                secret: "77e5b471f113f286d4e9c2f090eb5708",
                js_code: code,
                grant_type: "authorization_code"
            },
            method: 'GET',
            success: (result) => {
                resolve(result)
                // var reg = new RegExp("/", "g");
                // var reg2 = new RegExp("1", "g");
                // var data = result.data.replace(reg, "").replace(reg2, '');
                // var obj = JSON.parse(data);
                // resolve(obj);
            },
            fail: function(err) {
                reject(err);
            }

        });
    })
}

export const getSetting = () => {

    return new Promise((resolve, reject) => {
        wx.getSetting({
            success: (result) => {
                resolve(result)
            },
            fail: (err) => {
                reject(err)
            },
            complete: () => {}
        });
    })
}

export const chooseAddress = () => {

    return new Promise((resolve, reject) => {
        wx.chooseAddress({
            success: (result) => {
                resolve(result)
            },
            fail: (err) => {
                reject(err)
            },
            complete: () => {}
        });
    })
}


export const openSetting = () => {

    return new Promise((resolve, reject) => {
        wx.openSetting({
            success: (result) => {
                resolve(result)
            },
            fail: (err) => {
                reject(err)
            },
            complete: () => {}
        });
    })
}

export const setStorage = (key, data) => {

    return new Promise((resolve, reject) => {
        wx.setStorage({
            key: key,
            data: data,
            success: (result) => {
                resolve(result)
            },
            fail: (err) => {
                reject(err)
            },

        });
    })
}

export const getUserProfile = (str) => {
    return new Promise((resolve, reject) => {
        wx.getUserProfile({
            desc: str, //不写不弹提示框
            success: function (res) {
                resolve(res)
            },
            fail: function (err) {
                reject(err)
            }
          })
    })
}

